<template>
  <div class="dialog">
    <el-dialog
      :title="title"
      width="652px"
      :visible.sync="isShow"
      :before-close="handleClose"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <el-form ref="form" label-width="120px" :model="form" :rules="rules">
        <el-form-item label="资方编码" prop="code" v-if="form.id">
          <el-input disabled v-model="form.code"></el-input>
        </el-form-item>
        <el-form-item label="授信审批" prop="applyAuditStep">
          <el-radio-group v-model="form.applyAuditStep">
            <el-radio
              :disabled="form.id ? true : false"
              :label="item.value"
              v-for="(item, index) in approvalList"
              :key="index"
            >
              {{ item.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="支用审批" prop="drawdownAuditStep">
          <el-radio-group v-model="form.drawdownAuditStep">
            <el-radio
              :disabled="form.id ? true : false"
              :label="item.value"
              v-for="(item, index) in approvalList"
              :key="index"
            >
              {{ item.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="资方名称" prop="name">
          <el-input placeholder="请填写资方名称" v-model="form.name" maxlength="24"></el-input>
        </el-form-item>
        <el-form-item label="资方状态" prop="status">
          <el-select v-model="form.status">
            <el-option
              v-for="(item, index) in statusList"
              :key="index"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <div class="dialog-layout">
          <el-form-item label="资方logo" prop="logo">
            <FileUpload
              ref="FileUpload"
              accept="image/jpeg,image/png"
              @uploadSuccess="logoUploadSuccess"
              :imgUrl="form.logo"
            ></FileUpload>
          </el-form-item>
          <el-form-item label="资方长logo" prop="longLogo">
            <FileUpload
              ref="FileUpload"
              accept="image/jpeg,image/png"
              @uploadSuccess="longLogoUploadSuccess"
              :imgUrl="form.longLogo"
            ></FileUpload>
          </el-form-item>
        </div>
        <el-form-item label="描述" prop="description">
          <el-input type="textarea" maxlength="150" show-word-limit :rows="10" v-model="form.description"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="btn" @click="submit()">保存</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import FileUpload from '@/components/FileUpload/index.vue'
import { STATUS_LIST } from '@/constants/baseModel.js'
export default {
  name: 'AddEditPop',
  components: {
    FileUpload,
  },
  data() {
    return {
      approvalList: [
        { label: '仅一次审核', value: 1 },
        { label: '二次审核', value: 2 },
      ],
      rules: {
        name: [{ required: true, message: '请输入资方名称' }],
        applyAuditStep: [{ required: true, message: '请选择审核方式' }],
        drawdownAuditStep: [{ required: true, message: '请选择审核方式' }],
        logo: [{ required: true, message: '请上传logo' }],
        longLogo: [{ required: true, message: '请上传长logo' }],
      },
      statusList: STATUS_LIST,
      isShow: false,
      form: {
        id: '',
        name: '',
        code: '',
        status: '1',
        logo: '',
        longLogo: '',
        description: '',
        // 授信审核
        applyAuditStep: null,
        // 支用审核
        drawdownAuditStep: null,
      },
    }
  },
  computed: {
    title() {
      return this.form.id ? '编辑资方' : '新增资方'
    },
  },
  methods: {
    show(item) {
      this.isShow = true
      this.form.id = (item && item.id) || ''
      this.form.name = (item && item.name) || ''
      this.form.code = (item && item.code) || ''
      this.form.status = (item && item.status) || '1'
      this.form.logo = (item && item.logo) || ''
      this.form.longLogo = (item && item.longLogo) || ''
      this.form.description = (item && item.description) || ''
      this.form.applyAuditStep = (item && item.applyAuditStep) || null
      this.form.drawdownAuditStep = (item && item.drawdownAuditStep) || null
      console.log(this.form)
    },
    submit() {
      this.$refs['form'].validate(async valid => {
        if (!valid) return
        console.log(this.form)
        await this.sendSubmitRequest()
        this.resetData()
        this.$emit('popClose', true)
      })
    },
    async sendSubmitRequest() {
      const url = this.form.id ? `/funds/${this.form.id}/update` : `/funds/add`
      const method = this.form.id ? 'put' : 'post'
      await this.$http[method](url, this.form)
    },
    handleClose() {
      this.resetData()
    },
    resetData() {
      this.isShow = false
      this.form.id = ''
      this.form.name = ''
      this.form.code = ''
      this.form.status = '1'
      this.form.logo = ''
      this.form.longLogo = ''
      this.form.description = ''
      this.form.applyAuditStep = null
      this.form.drawdownAuditStep = null
      this.$refs['form'].resetFields()
    },
    logoUploadSuccess(res, file) {
      this.form.logo = res.data
      this.$refs.form.validateField('logo')
    },
    longLogoUploadSuccess(res, file) {
      this.form.longLogo = res.data
      this.$refs.form.validateField('longLogo')
    },
  },
}
</script>

<style lang="scss" scoped>
.dialog {
  &-layout {
    @include flex-space-between();
    width: 520px;
  }
}
.btn {
  width: 400px;
}
::v-deep {
  .el-input {
    width: 400px;
  }
  .el-input__inner {
    width: 400px;
  }
  .el-textarea {
    width: 400px;
  }
}
</style>
